/**
 * 
 */
package com.vision.core.cl.ui;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * @author Mark
 *
 */
public class Notifications {
	
	private static RootPanel messagePanel = RootPanel.get("messages");
	
	public static void clearMessage() {
		messagePanel.getElement().setInnerHTML("");
	}
	
	public static void info(String message) {
		messagePanel.getElement().setInnerHTML(message);
		hideAfterMillis(7000);
	}
	
	public static void error(String header, String message) {
		messagePanel.getElement().setInnerHTML(header + ": " + message);
		hideAfterMillis(10000);
	}
	
	public static void rpcError(Exception t) {
		messagePanel.getElement().setInnerHTML(t.getMessage());
		hideAfterMillis(10000);
	}
	
	public static void unexpectedError(Throwable t) {
		Log.error(t.getMessage(), t);
		messagePanel.getElement().setInnerHTML("An unexpected error occurred.");
		hideAfterMillis(10000);
	}
	
	public static void codeDownloadFailure(Throwable t) {
		messagePanel.getElement().setInnerHTML("Downloading of code failed. " + t.getMessage());
		hideAfterMillis(10000);
	}
	
	private static void hideAfterMillis(int time) {
		Timer timer = new Timer() {
			@Override
			public void run() {
				messagePanel.getElement().setInnerHTML("");
			}
		};
		timer.schedule(time);
	}

}
